<?php

class Authresourcegroup extends AppModel {

    var $belongsTo = array(
        'Parentresourcegroup' => array(
            'className' => 'Authresourcegroup',
            'foreignKey' => 'parent_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    var $hasMany = array(
        'Authresource' => array(
            'className' => 'Authresource',
            'foreignKey' => 'authresourcegroup_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Childresourcegroup' => array(
            'className' => 'Authresourcegroup',
            'foreignKey' => 'parent_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    function add($groupname, $groupparent) {

        if (empty($groupname))
            return INSUFFICIENT_PARAMETERS;
        $this->recursive = -1;
        $arr_resourcegroup = $this->find('count', array('conditions' => array('authresourcegroup.name' => $groupname)));
        if ($arr_resourcegroup != '0') {
            return NAME_ALREADY_TAKEN; // return error : name already taken
        } else {
            if ($groupparent) {
                $this->recursive = -1;
                $arr_resourcegroup = $this->find('first', array('conditions' => array('authresourcegroup.name' => $groupparent)));

                if (!empty($arr_resourcegroup)) {
                    $parent_id = $arr_resourcegroup['Authresourcegroup']['id'];
                    $field = array('name' => $groupname, 'parent_id' => $parent_id);
                } else {
                    return NON_EXISTANT_NAME; // return error : parent non-existant
                }
            } else {
                $field = array('name' => $groupname);
            }
            $this->create();
            if ($this->save($field)) {
                return SUCCESS; // return : success
            } else {
                return SAVE_ERROR; // return error : error saving
            }
        }
    }

    function edit($groupname, $newname, $newparent) {
        if (empty($groupname) || empty($newname)) {
            return INSUFFICIENT_PARAMETERS; // return error : error varible empty 	
        } else {
            $this->recursive = -1;
            $arr_resourcegroup = $this->find('first', array('conditions' => array('authresourcegroup.name' => $groupname)));
            if (empty($arr_resourcegroup)) {
                return NON_EXISTANT_NAME;
            } else {
                $resourcegroup_id = $arr_resourcegroup['Authresourcegroup']['id'];
            }
            $this->recursive = -1;
            $arr_resourcegroup = $this->find('first', array('conditions' => array('authresourcegroup.name' => $newname)));
            if (!empty($arr_resourcegroup)) {
                if ($resourcegroup_id != $arr_resourcegroup['Authresourcegroup']['id'])
                    return NAME_ALREADY_TAKEN;
            }

            if ($newparent) {
                $this->recursive = -1;
                $arr_resourcegroup = $this->find('first', array('conditions' => array('authresourcegroup.name' => $newparent)));
                $newparent_id = $arr_resourcegroup['Authresourcegroup']['id'];
                $field = array('id' => $resourcegroup_id, 'name' => $newname, 'parent_id' => $newparent_id);
            } else {
                $field = array('id' => $resourcegroup_id, 'name' => $newname, 'parent_id' => NULL);
            }
            //---------------------------------------------------------------------//
            if ($this->save($field)) {
                return SUCCESS; 		   
            } else {
                return SAVE_ERROR; 			   
            }
        }
    }

    function deleteData($groupname, $deletechildren) {
        if (empty($groupname)) {
            return INSUFFICIENT_PARAMETERS; // return error : error varible empty 
        }
        $this->recursive = -1;
        $arr_resourcegroup = $this->find('first', array('conditions' => array('authresourcegroup.name' => $groupname)));
        if (!empty($arr_resourcegroup)) {
            $group_id = $arr_resourcegroup['Authresourcegroup']['id'];
            if (!$this->del($group_id)) {
                return SAVE_ERROR;
            } else {
                return SUCCESS;
            }
        } else {
            return NON_EXISTANT_NAME; // return error : register not found
        }
    }

    function listdata($field, $value, $operator, $orderfield, $orderby, $limit, $page, $recursive) {

        if (!$recursive)
            $recursive = -1;
        $order = array();
        $conditions = array();

        if ($orderfield) {
            if (!$orderby) {
                $orderby = "";
            }
            $order = array("Authresourcegroup." . $orderfield . " " . $orderby);
        }
        if ($field) {

            $wildcard = "";
            if (!$operator)
                $operator = "";
            else {
                switch (strtoupper($operator)) {
                    case "=":
                        $operator = "";
                        break;
                    case "!=":
                        $operator = "<>";
                        break;
                    case "<>":
                        $operator = "<>";
                        break;
                    case "LIKE":
                        $operator = "LIKE";
                        $wildcard = "%";
                        break;
                    case "<":
                        $operator = "<";
                        break;
                    case ">":
                        $operator = ">";
                        break;
                    default:
                        $operator = "";
                        break;
                }
            }
            $conditions = array("Authresourcegroup." . $field . " " . $operator => $wildcard . $value . $wildcard);
        }


        if (!$limit) {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'page' => $page);
        }
        else {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit, 'page' => $page);
        }
        $this->recursive = $recursive;
        $resultsearch = $this->find('list', $params);
        return $resultsearch;
    }

    function getdata($field, $value, $operator, $orderfield, $orderby, $limit, $page, $recursive) {

        if (!$recursive)
            $recursive = -1;
        $order = array();
        $conditions = array();
        if ($orderfield) {

            if (!$orderby) {
                $orderby = "";
            }
            $order = array("Authresourcegroup." . $orderfield . " " . $orderby);
        }
        if ($field) {
            $wildcard = "";
            if (!$operator)
                $operator = "";
            else {
                switch (strtoupper($operator)) {
                    case "=":
                        $operator = "";
                        break;
                    case "!=":
                        $operator = "<>";
                        break;
                    case "<>":
                        $operator = "<>";
                        break;
                    case "LIKE":
                        $operator = "LIKE";
                        $wildcard = "%";
                        break;
                    case "<":
                        $operator = "<";
                        break;
                    case ">":
                        $operator = ">";
                        break;
                    default:
                        $operator = "";
                        break;
                }
            }
            $conditions = array("Authresourcegroup." . $field . " " . $operator => $wildcard . $value . $wildcard);
        }
        if (!$limit) {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'page' => $page);
        }
        else {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit, 'page' => $page);
        }
        $this->recursive = $recursive;
        $resultsearch = $this->find('all', $params);
        return $resultsearch;
    }

}

?>